/*
* $Id: ViewTable.cpp 48 2010-03-02 13:41:53Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewTable.h"
#include "ViewDefines.h"

CViewTable::CViewTable(CState* pState)
: CEmptyViewJournal(pState){}
CViewTable::~CViewTable(){}

void CViewTable::PaintView(CDC* dc, CRect rect){
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	dc->MoveTo(rect.TopLeft());
	dc->LineTo(CPoint(rect.right - 1, rect.top));
	dc->LineTo(CPoint(rect.right - 1, rect.bottom - 1));
	dc->LineTo(CPoint(rect.left - 1, rect.bottom - 1));
	pen.DeleteObject();
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	for(int i = 0; i < 3; i++){
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION, rect.top + 1, rect.left + DIST_BUTTON + i*BUTTON_REGION, rect.bottom - 1), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.top + 1,
			rect.left + (i+1)*BUTTON_REGION, rect.top + DIST_BUTTON), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.bottom - DIST_BUTTON,
			rect.left + (i+1)*BUTTON_REGION, rect.bottom - 1), &brush);
	}
	dc->FillRect(CRect(rect.left + 3*BUTTON_REGION, rect.top + 1, rect.right - 1, rect.bottom - 1), &brush);
	brush.DeleteObject();
}

BEGIN_MESSAGE_MAP(CViewTable, CEmptyViewJournal)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_SETCURSOR()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
END_MESSAGE_MAP()

afx_msg int CViewTable::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int ret = __super::OnCreate(lpCreateStruct);
	SetTimer(1, 10, NULL);
	m_buttonAdd.Create(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_ADD_ID);
	m_buttonDelete.Create(IApplication::GetApp()->GetLanguageManager()->GetString(DELETE_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_DELETE_ID);
	m_buttonChange.Create(IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_CHANGE_ID);
	m_tableRegion.Create(this);
	UpdateButtons();
	return ret;
}
afx_msg void CViewTable::OnDestroy(){
	KillTimer(1);
}
afx_msg void CViewTable::OnSize(UINT nType, int cx, int cy){
	CRect rect;
	GetClientRect(rect);
	m_buttonAdd.MoveWindow(CRect(rect.left + DIST_BUTTON, rect.bottom - CY_BUTTON - DIST_BUTTON,
		rect.left + BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_buttonChange.MoveWindow(CRect(rect.left + BUTTON_REGION + DIST_BUTTON,
		rect.bottom - 40, rect.left + 2*BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_buttonDelete.MoveWindow(CRect(rect.left + 2*BUTTON_REGION + DIST_BUTTON, rect.bottom - CY_BUTTON - DIST_BUTTON,
		rect.left + 3*BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	ResizeTableRegion(rect);
	__super::OnSize(nType, cx, cy);
}
afx_msg void CViewTable::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	PaintView(&dc, CRect(rect.left, rect.bottom - CY_BUTTON_REGION, rect.right, rect.bottom));
	m_tableRegion.Paint(&dc);
}

afx_msg BOOL CViewTable::OnSetCursor(CWnd*, UINT, UINT){
	CPoint posCur;
	GetCursorPos(&posCur);
	CRect rect;
	GetWindowRect(rect);
	posCur = posCur - rect.TopLeft();
	m_tableRegion.SetCursor(posCur);
	return TRUE;
}

afx_msg void CViewTable::OnTimer(UINT_PTR){
	if(m_tableRegion.IsResize()){
		m_tableRegion.Resize();
		Invalidate(FALSE);
	}
}

afx_msg void CViewTable::OnLButtonDown(UINT nFlags, CPoint point){
	if(m_tableRegion.MouseLBtnDown(point)){
		Invalidate(FALSE);
		UpdateButtons();
	}
	__super::OnLButtonDown(nFlags, point);
}

afx_msg void CViewTable::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	if(m_tableRegion.IsScroll(pScrollBar)){
		m_tableRegion.Scroll(nSBCode, pScrollBar);
		Invalidate(FALSE);
	}
	__super::OnHScroll(nSBCode, nPos, pScrollBar);
}

afx_msg void CViewTable::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	if(m_tableRegion.IsScroll(pScrollBar)){
		m_tableRegion.Scroll(nSBCode, pScrollBar);
		Invalidate(FALSE);
	}
	__super::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CViewTable::UpdateButtons(){
	if(m_tableRegion.IsSelectRow()){
		m_buttonChange.EnableWindow(TRUE);
		m_buttonDelete.EnableWindow(TRUE);
	}else{
		m_buttonChange.EnableWindow(FALSE);
		m_buttonDelete.EnableWindow(FALSE);
	}
}

void CViewTable::ResizeTableRegion(CRect rect){
	m_tableRegion.SetRect(CRect(rect.left, rect.top, rect.right, rect.bottom - CY_BUTTON_REGION));
}